'use strict';

const { options } = require('nodemon/lib/config');
// let products = require('../data/productList');
let { select, table, Op, db } = require('../db');

module.exports = {
    // 获取数据列表
    'get /product': async ctx => {
        let keyword = ctx.request.query.keyword;
        let attributes = { attributes: ['id', 'product_name', 'price', 'stock', 'supplier'] }
        let options = {
            order:['id']
        };
        if (keyword) {
            options.where = {
                [Op.or]: [
                    {supplier: {[Op.like]: `%${keyword}%`}},
                    {product_name: {[Op.like]: `%${keyword}%`}}
                ],
            }

            if (isNaN(keyword)){
                options.where[Op.or].push(
                db.where(db.fn('to_char', db.col('id'),'0'), keyword),
                db.where(db.fn('to_char', db.col('price'),'0'), keyword),
                db.where(db.fn('to_char', db.col('stock'),'0'), keyword));
                console.log(options);
            } else {
                options.where[Op.or].push( 
                    {'id': keyword},
                    {'price': keyword},
                    {'stock': keyword}
                );
            }
        }
        var data = await db.select(table.Products,attributes,options);

        ctx.body = data;
    },
    // 获取某条数据的信息
    'get /product/:id': async ctx => {
        let id = ctx.request.params.id;

        let data = await table.Products.findByPk(id);
        if (data) {
            ctx.body = {
                code: 1000,
                msg: 'success',
                data,
            }
        } else {
            ctx.body = {
                code: 400,
                msg: 'not found',
                data: null
            }
        }
    },
    // 插入
    'post /product': async ctx => {
        let data = ctx.request.body;
        console.log(data);

        let products = await db.insert(table.Products, data);
        ctx.body = products;
    },
    // 删除
    'delete /product/:id': async ctx => {
        let id = ctx.request.params.id;
        let data = await table.Products.destroy({where: {
            id
        }});
        if (data) {
            ctx.body = {
                code: 1000,
                msg: 'success',
                id
            }
        } else {
            ctx.body = {
                code: 400,
                msg: 'not found'
            }
        }
    },
    // 更新
    'put /product/:id': async ctx => {
        let id = ctx.request.params.id;

        // 判断请求是否指定了 id， 如果请求的id由用户指定，则拒绝
        if (ctx.request.body.id) {
            ctx.body = {
                code: 403,
                msg: 'Cannot chanage key: id'
            }

            return
        }

        // if (index !== -1) {
        //     let data = {};
        //     data.id = id;
        //     Object.assign(data, ctx.request.body)

        //     // 判断传入的对象中是否有无效的键
        //     for (const key in data) {
        //         if (data.hasOwnProperty(key)) {
        //             if (!products[0].hasOwnProperty(key)) {
        //                 ctx.body = {
        //                     code: 300,
        //                     msg: `Invalid key: ${key}`
        //                 }

        //                 return;
        //             }
        //         }
        //     }

            // 判断传入的数据中是否具有空值，如果有则将请求拒绝
            // for (const key in products[index]) {
            //     if (Object.hasOwnProperty.call(products[index], key)) {
            //         if (key !== 'id' && data[key] == '') {
            //             ctx.body = {
            //                 code: 403,
            //                 msg: `key:${key} is null!`
            //             }

            //             return;
            //         }
            //     }
            // }

            let data = await table.Products.update(ctx.request.body, {
                where: {
                    id
                }
            });
            if (data) {
                ctx.body = {
                code: 1000,
                msg: 'success'
            }
        } else {
            ctx.body = {
                code: 400,
                msg: 'not found'
            }
        }

    }
}